
#include "model/ClientNode.h"

namespace zeroplus::mq
{

    std::tuple<Bytes, bool> ClientNode::InvokeSync(const Bytes &message)
    {
        SendRequest(message);
        auto msg = RecvResponse();
        return msg;
    }

    std::tuple<Bytes, bool> ClientNode::InvokeAsync(const Bytes &message)
    {
        LOG_FATAL("not impl");
    }

    void ClientNode::InvokeOneWay(const Bytes &message)
    {
        bool res = SendRequest(message);
    }

    bool ClientNode::SendRequest(const Bytes &message)
    {
        zmqpp::message_t zmqMessage;
        zmqMessage << message.to_string();
        conn_->WriteMsg(zmqMessage);
    }

    std::tuple<Bytes, bool> ClientNode::RecvResponse()
    {
        auto [zmqMessage, res] = conn_->ReadMsg();
        std::string message;
        zmqMessage >> message;
        // LOG_DEBUG("recvBufSize = %d", conn_->GetKernelBufferSize());
        return std::make_tuple(Bytes::from_string(message), res);
    }

}
